//
//  SetViewController.m
//  MaxMovie
//
//  Created by Maxdong on 14-12-24.
//  Copyright (c) 2014年 lanou3g.com 蓝欧科技. All rights reserved.
//

#import "SetViewController.h"
#import "ParamLabelCell.h"
#import "ShowViewController.h"
#import "GYViewController.h"
#import "SCViewController.h"

#import "UIImageView+WebCache.h"
#import "SDImageCache.h"



@interface SetViewController ()

@end

@implementation SetViewController

-(void)awakeFromNib
{
    [super awakeFromNib];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
//        //-------   ShareSDK 注销
//        self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"注销"
//                                                                                   style:UIBarButtonItemStyleBordered
//                                                                                  target:self
//                                                                                  action:@selector(logoutButtonClickHandler:)]
//                                                  autorelease];
//        
        
        
        self.arr1 = [NSMutableArray arrayWithObjects:@"我的收藏",@"版本更新",@"清除缓存",@"夜间模式",@"喜欢我们，去评分",@"关于我们", nil];
        
        self.arr2 = [NSMutableArray arrayWithObjects:@"shoucang.png",@"gengxin.png", @"huancun.png", @"yejian.png",@"pingfen.png",@"guanyu.png", nil];
        
    }
    return self;
}


//- (void)viewWillAppear:(BOOL)animated
//{
//    if ([ShareSDK hasAuthorizedWithType:1]) {
//        [ShareSDK getUserInfoWithType:1 authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
//            [self.TXimage setImageWithURL:[NSURL URLWithString:[userInfo profileImage]] placeholderImage:[UIImage imageNamed:@"touxiang.png"]];
//        }];
//    }
//    
//    if ([ShareSDK hasAuthorizedWithType:2]) {
//        [ShareSDK getUserInfoWithType:2 authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
//            [self.TXimage setImageWithURL:[NSURL URLWithString:[userInfo profileImage]]placeholderImage:[UIImage imageNamed:@"touxiang.png"]];
//        }];
//    }
//}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 搜索条
    UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 30)];
    searchBar.tag = 1000;
    searchBar.backgroundColor = [UIColor redColor];
    
    //设置控件的委托
    searchBar.delegate = self;
    
    searchBar.barStyle = UIBarStyleDefault;
    searchBar.placeholder = @"输入影片名/影人名";
    //对于文本对象自动校正风格
    searchBar.autocorrectionType = UITextAutocapitalizationTypeNone;
    //键盘的样式
    searchBar.keyboardType = UIKeyboardAppearanceDefault;
    
    [self.view addSubview:searchBar];
    [searchBar release];
    
    //登录imageview
    UIImageView *myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 94, 320, 100)];
    myImageView.backgroundColor = [UIColor whiteColor];
    [myImageView setUserInteractionEnabled:YES];
    [self.view addSubview:myImageView];
    [myImageView release];
    
    
    // 新浪
    self.image1 = [[UIImageView alloc]initWithFrame:CGRectMake(45, 17, 40, 40)];
    self.image1.image = [UIImage imageNamed:@"weibo.png"];
    [myImageView addSubview:self.image1];
    [self.image1 release];
    
    self.button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button2.frame = CGRectMake(10, 50, 110, 60);
    [self.button2 setTitle:@"新浪微博登录" forState:UIControlStateNormal];
    [self.button2 setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
    [self.button2 addTarget:self action:@selector(button2Clicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [myImageView addSubview:self.button2];
    
    //腾讯
    self.image2 = [[UIImageView alloc]initWithFrame:CGRectMake(235, 13, 45, 45)];
    self.image2.image = [UIImage imageNamed:@"qq.png"];
    [myImageView addSubview:self.image2];
    [self.image2 release];
    
    self.button3 = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button3.frame = CGRectMake(210, 50, 100, 60);
    [self.button3 setTitle:@"腾讯微博登录" forState:UIControlStateNormal];
    [self.button3 setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
    [self.button3 addTarget:self action:@selector(button3Clicked:) forControlEvents:UIControlEventTouchUpInside];
    [myImageView addSubview:self.button3];
    
    
    //头像
    self.TXimage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"touxiang.png"]];
    self.TXimage.frame = CGRectMake(120, 5, 80, 80);
    self.TXimage.userInteractionEnabled = YES;
    [myImageView addSubview:self.TXimage];
    [self.TXimage release];
    
    
    UITableView *tmpTableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 210, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    tmpTableView.dataSource = self;
    tmpTableView.delegate = self;
    [tmpTableView setScrollEnabled:NO];
    [tmpTableView setSeparatorStyle:NO];
    [tmpTableView registerClass:[ParamLabelCell class] forCellReuseIdentifier:@"reuse"];
    [self.view addSubview:tmpTableView];
    self.myTableView = tmpTableView;
    [tmpTableView release];
    
    
    //左右button
//    UIButton * leftb =[UIButton buttonWithType:UIButtonTypeSystem];
//    leftb.frame =CGRectMake(20, 20, 20, 20);
//    UIImage * img1 =[UIImage imageNamed:@"fanhui.png"];
//    [leftb setBackgroundImage:img1 forState:UIControlStateNormal];
//    [leftb addTarget:self action:@selector(leftbAction:) forControlEvents:UIControlEventTouchUpInside];
//    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:leftb];
//    //[leftb release];
    
    
    UIButton * rightb =[UIButton buttonWithType:UIButtonTypeSystem];
    rightb.frame =CGRectMake(280, 20, 40, 30);
    [rightb setTitle:@"注销" forState:UIControlStateNormal];
    [rightb setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [rightb addTarget:self action:@selector(rightbAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:rightb];
    //[rightb release];

    
}

//左右button事件
- (void)leftbAction:(id)sender
{
    
    //ShowViewController * show =[[ShowViewController alloc] init];
   // [self dismissViewControllerAnimated:YES completion:nil];
    
    
    
}
- (void)rightbAction:(id)sender
{

    
    
}






-(void)button2Clicked:(UIButton *)button
{
}

-(void)button3Clicked:(UIButton *)button
{
    
}




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr1.count ;
    
}


//代理 先判断 1.  有几个section 2. 每个section 多少个cell

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"reuse";
    ParamLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    cell.leftLabel.text = [self.arr1 objectAtIndex:indexPath.row];
    cell.leftImageView.image = [UIImage imageNamed:[self.arr2 objectAtIndex:indexPath.row]];
    
    //收藏
    if (indexPath.row == 0) {
        self.SCImage = [[UIImageView alloc]initWithFrame:CGRectMake(270, 15, 20, 20)];
        self.SCImage.image = [UIImage imageNamed:@"jiantou.png"];
        [cell addSubview:self.SCImage];
        [self.SCImage release];
    }
    
    
    //版本更新
    if (indexPath.row == 1) {
        self.GYLabel = [[UILabel alloc]initWithFrame:CGRectMake(210, 10, 100, 30)];
        self.GYLabel.text = @"当前是最新版本";
        self.GYLabel.font = [UIFont boldSystemFontOfSize:12];
        [self.GYLabel setTextColor:[UIColor purpleColor]];
        [cell addSubview:self.GYLabel];
        [self.GYLabel release];
    }
    
    
    //清除缓存
    if (indexPath.row == 2) {
        self.cacheLabel = [[UILabel alloc]initWithFrame:CGRectMake(250, 10, 100, 30)];
        self.cacheLabel.text = [self sizeCache];
        [self.cacheLabel setTextColor:[UIColor purpleColor]];
        self.cacheLabel.font = [UIFont boldSystemFontOfSize:12];
        
        [cell addSubview:self.cacheLabel];
        
    }
    
    
    //夜间模式
    if (indexPath.row == 3) {
        UISwitch *YJSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(250, 10, 100, 0)];
        [YJSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventEditingChanged];
        [cell addSubview:YJSwitch];
        [YJSwitch release];
        
    }
    
    //评分
    if (indexPath.row == 4) {
        self.PFImage = [[UIImageView alloc]initWithFrame:CGRectMake(270, 15, 20, 20)];
        self.PFImage.image = [UIImage imageNamed:@"jiantou.png"];
        [cell addSubview:self.PFImage];
        [self.PFImage release];
    }
    
    
    //关于我们
    if (indexPath.row == 5) {
        self.USImage = [[UIImageView alloc]initWithFrame:CGRectMake(270, 15, 20, 20)];
        self.USImage.image = [UIImage imageNamed:@"jiantou.png"];
        [cell addSubview:self.USImage];
        [self.USImage release];
    }
    return cell;
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.myTableView deselectRowAtIndexPath:indexPath animated:YES];
    //我的收藏
    if (indexPath.row == 0) {
        SCViewController *SCvc = [[SCViewController alloc]init];
        [self.navigationController  pushViewController:SCvc animated:YES];
    }
    
    
    //版本更新
    if (indexPath.row == 1) {
        UIAlertView *BBAlert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"当前已是最新版本"delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [BBAlert show];
        [self.view addSubview:BBAlert];
        [BBAlert release];
    }
    
    //清除缓存
    if (indexPath.row == 2) {
        UIAlertView *HCAlert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"您确定要清除缓存吗" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [HCAlert show];
        [self.view addSubview:HCAlert];;
        [HCAlert release];
    }
    
    
    //关于我们
    if (indexPath.row == 5) {
        GYViewController *GYcontroller = [[GYViewController alloc]init];
        [self.navigationController pushViewController:GYcontroller animated:YES];
    }
    
    
    
}

-(void)switchAction:(UISwitch *)YJSwitch
{
    
}
//搜索 键盘撤回
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UISearchBar *searchBar = (UISearchBar *)[self.view viewWithTag:1000];
    [searchBar resignFirstResponder];
}

// 计算缓存文件夹大小
- (long)fileSizeForDir:(NSString*)path
{
    long size = 0;
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSArray* array = [fileManager contentsOfDirectoryAtPath:path error:nil];
    for(int i = 0; i < [array count]; i++)
    {
        NSString *fullPath = [path stringByAppendingPathComponent:[array objectAtIndex:i]];
        BOOL isDir;
        if ( !([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && isDir) )
        {
            NSDictionary *fileAttributeDic = [fileManager attributesOfItemAtPath:fullPath error:nil];
            size += fileAttributeDic.fileSize;
        }
        else
        {
            [self fileSizeForDir:fullPath];
        }
    }
    [fileManager release];
    return size;
    
}

// 计算缓存大小
- (NSString *)sizeCache
{
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    long docSize = [self fileSizeForDir:docPath];
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
    NSString *cachesDir = [paths objectAtIndex:0];
    NSString *ImageCachePath = [cachesDir stringByAppendingPathComponent:@"ImageCache"];
    long ImageCacheSize = [self fileSizeForDir:ImageCachePath];
    long totalSize = docSize + ImageCacheSize;
    const unsigned int bytes = 1024*1024 ;   //字节数，如果想获取KB就1024，MB就1024*1024
    NSString *string = [NSString stringWithFormat:@"%.2fMB",(1.0 *totalSize/bytes)];
    
    return string;
}


// 清除缓存的方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        dispatch_async(
                       dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 )
                       , ^{
                           NSString *cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                           NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                           for (NSString *p in files) {
                               NSError *error;
                               NSString *path = [cachPath stringByAppendingPathComponent:p];
                               if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                                   [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                               }
                           }
                           [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];});
    }
}
// 清理缓存最后调用的方法
- (void)clearCacheSuccess
{
    [self.cacheLabel setText:@"0.0MB"];
}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

